package com.etop.learnapp.activity;

import android.content.Intent;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.etop.learnapp.R;
import com.etop.learnapp.me.drakeet.materialdialog.MaterialDialog;
import com.etop.learnapp.util.BitmapCreate;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by John on 2015/12/2.
 */
public class PracticeActivity extends AppCompatActivity {
    LinearLayout mLienarLayout;
    List<View> list;
    private String ImageUrihome;
    private String HomeUri;
    private String AnsUri;
    private String ImageUrians;
    ImageView iv;
    String[] homeList;
    String[] answerList;
    Button bn;
    Button btnNext;
    int index = 0;
    String[] FileName;
    private Toolbar toolbar;
    private final TranslateAnimation tAnim = new TranslateAnimation(-900, 0, 0, 0);
    private final RotateAnimation rAnim = new RotateAnimation(-80, 0);
    private final ScaleAnimation sAnim = new ScaleAnimation(0, 1, 0, 1);
    private final AlphaAnimation aAnim = new AlphaAnimation(0.0f, 1.0f);
    private  Animation[] animations =null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_practice);
        initToolbar();
        initAnim();
        mLienarLayout = (LinearLayout) findViewById(R.id.ll_homework);
        btnNext = (Button) findViewById(R.id.next);
        list = new ArrayList<>();
        int chapterSelect = getIntent().getIntExtra("chapterSelect", -1);
        int sectionSelect = getIntent().getIntExtra("sectionSelect", -1);
        int keySelect = getIntent().getIntExtra("keySelect", -1);
        AssetManager am = getAssets();

        ImageUrihome = "picture/" + chapterSelect  + "_" + keySelect + "/homework";
        ImageUrians = "picture/" + chapterSelect  + "_" + keySelect + "/answer";
//        ImageUrihome = "picture/6_1_2"  + "/homework";
//        ImageUrians = "picture/6_1_2" + "/answer";
        int num = 0;
        try {
            FileName = am.list(ImageUrihome);
            num = FileName.length;
        } catch (IOException e) {
            e.printStackTrace();
        }
        for (int i = 1; i <=1; i++) {
            iv = new ImageView(this);
            HomeUri = "assets://" + ImageUrihome + "/" + i + ".png";
            AnsUri = "assets://" + ImageUrians + "/" + i + ".png";
            iv = new ImageView(this);
            iv.setScaleType(ImageView.ScaleType.FIT_START);
            iv.setAdjustViewBounds(true);
            new BitmapCreate(HomeUri, this, iv);
            iv.setOnClickListener(new View.OnClickListener() {
                                      String UR = HomeUri;

                                      @Override
                                      public void onClick(View v) {
                                          Intent intent = new Intent(PracticeActivity.this, PicActivity.class);
                                          intent.putExtra("Resources", UR);
                                          startActivity(intent);
                                      }
                                  }
            );
            list.add(iv);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT);
            lp.setMargins(5, 10, 5, 0);
            iv.setLayoutParams(lp);

            mLienarLayout.addView(iv);
            iv.setAnimation(animations[i%4]);

            if(FileName.length!=i && FileName[i].endsWith(".txt")){
                i++;
                TextView tv;
                String fileName = "picture/" + chapterSelect  + "_" + keySelect + "/homework/2.txt";
                InputStream is = null;
                try {
                    is = am.open(fileName);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                InputStreamReader isr = new InputStreamReader(is);
                BufferedReader br = new BufferedReader(isr);
                String line;
                StringBuilder sb = new StringBuilder();
                try {
                    while ((line = br.readLine()) != null) {
                        sb.append(line);
                        sb.append("\n");
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                tv = new TextView(this);
                tv.setText(sb);
                tv.setTextSize(18);
                list.add(tv);
                mLienarLayout.addView(tv);
                tv.setAnimation(animations[i%4]);
            }
            bn = new Button(this);
            bn.setText("查看答案");
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            bn.setLayoutParams(params);
            bn.setOnClickListener(new View.OnClickListener() {
                                      MaterialDialog mMaterialDialog;
                                      String UR = AnsUri;

                                      @Override
                                      public void onClick(View v) {
                                          mMaterialDialog = new MaterialDialog(PracticeActivity.this).setTitle("答案");
                                          ImageView ig = new ImageView(PracticeActivity.this);
                                          new BitmapCreate(UR, PracticeActivity.this, ig);
                                          mMaterialDialog.setView(ig);
                                          mMaterialDialog.setPositiveButton("OK", new View.OnClickListener() {
                                              @Override
                                              public void onClick(View v) {
                                                  mMaterialDialog.dismiss();
                                              }
                                          });
                                          mMaterialDialog.show();
                                      }
                                  }
            );
            mLienarLayout.addView(bn);
            list.add(bn);
        }
        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Snackbar.make(v, "没有下一题了", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }
    private void initToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar_include);
        setSupportActionBar(toolbar);
        setTitle(getIntent().getStringExtra("keyName"));
        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                onBackPressed();
            }
        });
    }
    private void initAnim() {
        tAnim.setDuration(800);
        rAnim.setDuration(800);
        sAnim.setDuration(800);
        aAnim.setDuration(800);
        animations = new Animation[]{sAnim,rAnim,aAnim,tAnim};
    }
    }

